package com.ye.study.controller;

import cn.hutool.core.util.IdUtil;

import com.ye.study.utils.PerformanceRunnerUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.TimeUnit;

/**
 * @auther zzyy
 * @create 2025-02-13 11:30
 */
@RestController
@Slf4j
public class PerformanceRunnerController
{

    public void print(PerformanceRunnerUtil.PerformanceRunnerResult retValue) {
        System.out.println("\n压测结果如下：");
        System.out.println("==============================");
        printFormat("请求总数", retValue.getRequests());
        printFormat("并发量", retValue.getConcurrency());
        printFormat("成功请求数", retValue.getSuccessRequests());
        printFormat("失败请求数", retValue.getFailRequests());
        printFormat("请求总耗时(ms)", retValue.getTimeTakenForTests());
        printFormat("每秒请求数(吞吐量)", retValue.getRequestsPerSecond());
        printFormat("每个请求平均耗时(ms)", retValue.getTimePerRequest());
        printFormat("最快的请求耗时(ms)", retValue.getFastestCostTime());
        printFormat("最慢的请求耗时(ms)", retValue.getSlowestCostTime());
        System.out.println("==============================");
    }

    public void printFormat(Object p1, Object p2) {
        System.out.println(String.format("%s: %s", p1, p2));
    }

    //=============下面是业务方法，用我们之前写的性能测试工具压测下面的test1和test2
    @GetMapping("/test1")
    public String test1()
    {
        log.info("test1  "+ IdUtil.fastSimpleUUID());
        return "ok";
    }

    @GetMapping("/test2")
    public String test2()
    {
        log.info("test2  "+ IdUtil.fastSimpleUUID());
        //暂停100毫秒
        try { TimeUnit.MILLISECONDS.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); }
        return "暂停100毫秒-ok2";
    }
    //http://localhost:24414/performance/test1
    @GetMapping(value = "/performance/test1")
    public void performanceTest1() throws InterruptedException
    {
        //总请求数
        int requests = 1000;
        //并发数量,类似银行窗口只有这么多
        int concurrency = 2;
        //RestTemplate
        RestTemplate restTemplate = new RestTemplate();
        //将对如下restful地址展开压测，我们的业务方法地址
        String url = "http://localhost:24414/test1";
        System.out.println("压测地址: "+url);
        //调用压测工具类开始压测
        PerformanceRunnerUtil.PerformanceRunnerResult result
                = PerformanceRunnerUtil
                .performanceRun(requests,concurrency,() -> restTemplate.getForObject(url, String.class));
        //输出压测结果
        print(result);
    }

    @GetMapping(value = "/performance/test2")
    public void performanceTest2() throws InterruptedException
    {
        //总请求数
        int requests = 1000;
        //并发数量,类似银行窗口只有这么多
        int concurrency = 100;
        //RestTemplate
        RestTemplate restTemplate = new RestTemplate();
        //将对如下restful地址展开压测，我们的业务方法地址
        String url = "http://localhost:24414/test2";
        System.out.println("压测地址: "+url);
        //调用压测工具类开始压测
        PerformanceRunnerUtil.PerformanceRunnerResult result =
                PerformanceRunnerUtil.
                        performanceRun(requests,concurrency,() -> restTemplate.getForObject(url, String.class));
        //输出压测结果
        print(result);
    }

}

